﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.DAL;

namespace Domain.BLL
{
    public class ProductBLL
    {
        public int     ProductID       {get; set;}
        public string   ProductName     {get; set;}
        public int?     CategoryID      {get; set;}        
        public int?     SupplierID      {get; set;}
        public Int64?   PriceIn         {get; set;}
        public Int64?   SalePrice { get; set; }
        public byte?     WarrantyTime    {get; set;}
        public int?    QuantityInStock {get; set;}
        
        public string   ImageName         {get; set;}
        public string   Description     {get; set;}
        public bool isAvailable { get; set; }
        public int? StockLimit { get; set; }

        //public List<ProductBLL> ProductList{get; set;}

        public IEnumerable<ProductBLL> listAllProducts()
        {
            ProductDAL _p = new ProductDAL();
            return _p.listOfAllProducts().ToList();
        }

        public void SingleRead()
        {
            //Product _product;
            using (var ctx = new CSEntities())
            {
                var _product = (from _p in ctx.Products
                               where _p.ProductID == this.ProductID
                               select _p).First();

                if (_product != null)
                {
                    this.ProductID = _product.ProductID;
                    this.SupplierID = _product.SupplierID;
                    this.CategoryID = _product.CategoryID;
                    this.ProductName = _product.ProductName;
                    this.Description = _product.Description;
                    this.PriceIn = _product.PriceIn;
                    this.SalePrice = _product.SalePrice;
                    this.WarrantyTime = _product.WarrantyTime;
                    this.isAvailable = _product.isAvailable;
                    this.ImageName = _product.ImageName;
                    this.QuantityInStock = _product.QuantityInStock;
                    this.StockLimit = _product.StockLimit;

                }

            }
             
        }

        public void Update()
        {
            using (var ctx = new CSEntities())
            {
                Product _product = (from _p in ctx.Products
                                where _p.ProductID == this.ProductID
                                select _p).First();

                if (_product != null)
                {
                    _product.ProductName = this.ProductName;
                    _product.Description = this.Description;
                    _product.PriceIn = this.PriceIn;
                }

                ctx.SaveChanges();
            }
        }
        //public int ProductID { get; set; }



        public void SingleDelete()
        {
            using (var ctx = new CSEntities())
            {
                Product _product = (from _p in ctx.Products
                                    where _p.ProductID == this.ProductID
                                    select _p).First();

                if (_product != null)
                {
                    ctx.DeleteObject(_product);
                    ctx.SaveChanges();
                }

                
            }
            //throw new NotImplementedException();
        }

        public IEnumerable<ProductBLL> ListProductByCategory(int CatId)
        {
            if (CatId == 7)
                return listAllProducts();
            
            ProductDAL _p = new ProductDAL();
            return _p.ListProductByCategory(CatId).ToList();
        }
    }
}
